/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.game;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import rpg.Location;
import static rpg.RPG.*;
import rpg.entities.Entity;
import rpg.entities.LivingEntity;
import rpg.entities.Player;
import rpg.entities.area.Renderable;
import rpg.gfx.Bitmap;
import rpg.gfx.gui.Bar;
import rpg.gfx.gui.GUI;
import rpg.gfx.light.LightSource;
import rpg.gfx.light.Lighting;
import rpg.util.SortedList;

/**
 * rothens.tarhely.biz
 *
 * @author Rothens
 */
public class GameScreen extends Bitmap {

    public GUI gui;
    private static GameScreen instance;
    private Bitmap viewport;
    int time = 0;
    public int x = 0;
    public int y = 0;

    public List<LightSource> getLightSources(Renderable r) {
        List<LightSource> ret = new LinkedList<LightSource>();
        for (LightSource ls : SingleGame.getInstance().getActual().lightSources) {
            Location l = ls.getLocation();
            Location topLeft = r.topLeft;
            Location bottomRight = r.bottomRight;
            if (l.getTileX() >= topLeft.getTileX() - Lighting.MAX_DISTANCE
                    && l.getTileY() >= topLeft.getTileY() - Lighting.MAX_DISTANCE
                    && l.getTileX() <= bottomRight.getTileX() + Lighting.MAX_DISTANCE
                    && l.getTileY() <= bottomRight.getTileY() + Lighting.MAX_DISTANCE) {
                ret.add(ls);
            }
        }
        return ret;
    }

    public synchronized static GameScreen getInstance() {
        return instance;
    }

    public GameScreen(int width, int height) {
        super(width, height);
        instance = this;
        gui = new GUI(width, height);
        viewport = new Bitmap(width, height);
    }

    public void render(SingleGame game) {
        time++;

        //Player player = Player.getInstance();
        //if (player != null) {
        //    if (player.getCurrentLocation() != null) {
        //        Location loc = player.getCurrentLocation();
        //        x = (loc.getX()) + player.getxRenderOffset();
        //        y = (loc.getY()) + player.getyRenderOffset();
        if (game == null) {
            System.out.println("game null");
        }
        if (game == null || game.getCamera() == null) {
            return;
        }
        x = game.getCamera().getX();
        y = game.getCamera().getY();
        Renderable r = new Renderable(SingleGame.getInstance().getActual(), x, y);
        int xdiff = ((x - (int) (WINDOW_WIDTH / 2)) % 64);
        int ydiff = ((y - (int) (WINDOW_HEIGHT / 2)) % 64);
        for (int iy = 0; iy < r.height; iy++) {
            for (int ix = 0; ix < r.width; ix++) {
                int t = iy * r.width + ix;
                int dx = ix * 64 - xdiff;
                int dy = iy * 64 - ydiff;
                viewport.draw(r.tiles[t].getOverImage(), dx, dy);
            }


        }
        SortedList<Entity> sortedList = new SortedList<Entity>(new Comparator<Entity>() {
            @Override
            public int compare(final Entity a, final Entity b) {
                if (a.getCurrentLocation().getY() > b.getCurrentLocation().getY()) {
                    return 1;
                }
                if (a.getCurrentLocation().getY() < b.getCurrentLocation().getY()) {
                    return -1;
                }
                return 0;
            }
        });
        for (Entity e : SingleGame.getInstance().actual.entities) {
            if (e.isInView(r)) {
                sortedList.add(e);
            }
        }
        for (Entity e : sortedList) {
            viewport.draw(e.getDrawGraphics(), e.getxRenderOffset() - 32, e.getyRenderOffset() - 32);
            if (e instanceof LivingEntity) {
                if (e.equals(Player.getInstance())) {
                    continue;
                }
                Bar health = ((LivingEntity) e).getHealthBar();
                viewport.draw(health, e.getxRenderOffset() - 16, e.getyRenderOffset() - 32);
            }
        }

        for (int i = 0; i < r.width * r.height; i++) {
            int dx = (i % r.width) * 64 - ((x - WINDOW_WIDTH / 2) % 64);
            int dy = (i / r.width) * 64 - ((y - WINDOW_HEIGHT / 2) % 64);
            if (r.tiles[i].getLayer2() != null) {
                viewport.draw(r.tiles[i].getLayer2().bitmap, dx, dy);
            }
        }
        //    }
        //}
        gui.renderGUI();
        if (SingleGame.getInstance().isPaused()) {
            viewport.desaturate();
        }
        viewport.draw(gui, 0, 0);

        draw(viewport, 0, 0);

    }
}
